package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.Member;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * Member DAO
 * 
 * @author Administrator
 * 
 */
public class MemberDao {

	DBHelper dbHelper;
	private final static String LOG_TAG = "MemberDao";

	protected static final String TBL_NAME = "member";

	public MemberDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new member
	 * 
	 * @param member
	 */
	public String save(Member member) {
		
		member.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		member.setCreated(time);
		member.setModified(time);
		Log.i(LOG_TAG, "MemberDao  save " + member.toString());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into member(ID,CardID,Points,Name,ShfzID,Phone,Address,created,modified) values(?,?,?,?,?,?,?,?,?)",
					new Object[] { member.getId(), member.getCardId(),
							 member.getPoints(),
							member.getName(), member.getShfzID(),
							member.getPhone(), member.getAddress(),
							member.getCreated(), member.getModified() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("MemberDao  save", e.getMessage());
		}
		finally {
			db.endTransaction();// 事务结束;
			db.close();
		}
		return member.getId();
	}

	/**
	 * update a member
	 * 
	 * @param member
	 */

	public void update(Member member) {
		Log.i(LOG_TAG, "MemberDao  update " + member.toString());
		member.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"update member set CardID=?,Points=?,Name=?,ShfzID=?,Phone=?,Address=?,modified=?  where ID=?",
					new Object[] { member.getCardId(), 
							member.getPoints(), member.getName(),
							member.getShfzID(), member.getPhone(),
							member.getAddress(), member.getModified(),
							member.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("MemberDao  update", e.getMessage());
		}
		finally {
			db.endTransaction();// 事务结束;
			db.endTransaction();// 事务结束;
			db.close();
		}
		
	
	}

	/**
	 * get a member by id
	 * 
	 * @param i
	 * @return
	 */
	public Member find(String id) {
		Log.i(LOG_TAG, "MemberDao  find " + id);
		Member member =null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,CardID,Points,Name,ShfzID,Phone,Address,created,modified from member where ID=?",
						new String[] { id });
		if (cursor.moveToNext()) {
			member = new Member();

			member.setId(cursor.getString(cursor.getColumnIndex("ID")));
			member.setCardId(cursor.getString(1));
			member.setPoints(cursor.getInt(2));
			member.setName(cursor.getString(3));
			member.setShfzID(cursor.getString(4));
			member.setPhone(cursor.getString(5));
			member.setAddress(cursor.getString(6));
			member.setCreated(cursor.getLong(7));
			member.setModified(cursor.getLong(8));
			
		}
		cursor.close();
		db.close();
		return member;
	}
	/**
	 * get a member by id
	 * 
	 * @param i
	 * @return
	 */
	public Member findMemberByShfzId(String id) {
		Log.i(LOG_TAG, "MemberDao  find " + id);
		Member member =null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,CardID,Points,Name,ShfzID,Phone,Address,created,modified from member where ShfzID=?",
						new String[] { id });
		if (cursor.moveToNext()) {
			member = new Member();

			member.setId(cursor.getString(cursor.getColumnIndex("ID")));
			member.setCardId(cursor.getString(1));
			member.setPoints(cursor.getInt(2));
			member.setName(cursor.getString(3));
			member.setShfzID(cursor.getString(4));
			member.setPhone(cursor.getString(5));
			member.setAddress(cursor.getString(6));
			member.setCreated(cursor.getLong(7));
			member.setModified(cursor.getLong(8));
			
		}
		cursor.close();
		db.close();
		return member;
	}
	/**
	 * delete a member by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from member where ID=?", new Object[] { id });
		db.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "  delete all from  member: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete  from member");
		db.close();
	}

	/**
	 * fetch members
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<Member> getScrollData(int pageNumber, int pageSize) {
		List<Member> members = new ArrayList<Member>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,CardID,Points,Name,ShfzID,Phone,Address,created,modified from member order by modified desc limit ?,?",
						new String[] {
								String.valueOf((pageNumber - 1) * pageSize),
								String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Member member = new Member();
			member.setId(cursor.getString(cursor.getColumnIndex("ID")));
			member.setCardId(cursor.getString(1));
			member.setPoints(cursor.getInt(2));
			member.setName(cursor.getString(3));
			member.setShfzID(cursor.getString(4));
			member.setPhone(cursor.getString(5));
			member.setAddress(cursor.getString(6));
			member.setCreated(cursor.getLong(7));
			member.setModified(cursor.getLong(8));
			members.add(member);
		}
		cursor.close();
		db.close();
		return members;
	}
	/**
	 * fetch members
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public ArrayList<Member> getScrollData(long btime, long etime,int pageNumber, int pageSize) {
		ArrayList<Member> members = new ArrayList<Member>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select member.ID,member.CardID,Points,Name,ShfzID,Phone,Address,member.created,member.modified, card.CardNumber from member,card where member.CardID =card.ID and  member.created >="+btime+" and member.created <="+etime+" order by member.modified desc limit ?,?",
						new String[] {
								String.valueOf((pageNumber - 1) * pageSize),
								String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Member member = new Member();
			member.setId(cursor.getString(cursor.getColumnIndex("ID")));
			member.setCardId(cursor.getString(1));
			member.setPoints(cursor.getInt(2));
			member.setName(cursor.getString(3));
			member.setShfzID(cursor.getString(4));
			member.setPhone(cursor.getString(5));
			member.setAddress(cursor.getString(6));
			member.setCreated(cursor.getLong(7));
			member.setModified(cursor.getLong(8));
			member.setCardNumber(cursor.getString(9));
			members.add(member);
		}
		cursor.close();
		db.close();
		return members;
	}
	
	/**
	 * fetch members
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public int getCount(long btime, long etime) {
		int count = 0;
		
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select count(member.ID) from member,card where member.CardID =card.ID and  member.created >="+btime+" and member.created <="+etime+" ",
						null);// pageNumber页码
        if(cursor.moveToNext()){
        	count = cursor.getInt(0);
        }
        cursor.close();
		db.close();
		return count;
	}
	/**
	 * fetch all members
	 * 
	 * @return
	 */
	public List<Member> getAll() {
		List<Member> members = new ArrayList<Member>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select member.ID,member.CardID,member.Points,member.Name,member.ShfzID,member.Phone,member.Address,member.created,member.modified,card.CardNumber from member,card where member.CardID = card.ID order by member.modified desc",
						null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Member member = new Member();
			member.setId(cursor.getString(cursor.getColumnIndex("ID")));
			member.setCardId(cursor.getString(1));
			member.setPoints(cursor.getInt(2));
			member.setName(cursor.getString(3));
			member.setShfzID(cursor.getString(4));
			member.setPhone(cursor.getString(5));
			member.setAddress(cursor.getString(6));
			member.setCreated(cursor.getLong(7));
			member.setModified(cursor.getLong(8));
			member.setCardNumber(cursor.getString(9));
			members.add(member);
		}
		cursor.close();
		db.close();
		return members;
	}

	/**
	 * get member's count
	 * 
	 * @return
	 */
	public long getCount() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		db.close();
		return count;
	}

}
